package com.rpg.battle;

import com.rpg.Drawable_I;
import com.rpg.R;
import com.rpg.battle.menu.BattleMenu;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class BattleView extends View implements Drawable_I, View.OnTouchListener{
	public BattleView(Activity act) {
		super(act);
		fTeam = new Team(act, 0, 1, act);
		eTeam = new Team(act, (int)(act.getResources().getDisplayMetrics().widthPixels * 0.90), -1, act);
		addBattleMenu(act);
   	}
	
	private void addBattleMenu(Activity activity) {
		RelativeLayout layout = new RelativeLayout(activity);
        layout.addView(this);

        LinearLayout innerLayout = new LinearLayout(activity);
        layout.addView(innerLayout);
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)innerLayout.getLayoutParams();
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        lp.setMargins(0, 0, 0, 0);
        
        Matrix matrix = new Matrix();
        DisplayMetrics display = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(display);
        
		int width = 0; 
		int height = 0;
        int newWidth = (int) (display.widthPixels * .2);
        int newHeight = (int) (display.heightPixels * .2);
        
        float scaleWidth = 0f;
        float scaleHeight = 0f; 
        Bitmap bitmap;
        ImageButton ibut;
        
        /*
         * Attack button with image
         * 
         */
        
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.attack);
        width = bitmap.getWidth();
        height = bitmap.getHeight();
        scaleHeight = ((float) newHeight) / height;
        scaleWidth = ((float) newWidth) / width;
        ibut = new ImageButton(activity);
        matrix.postScale(scaleWidth, scaleHeight);
        ibut.setImageDrawable(new BitmapDrawable(Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true)));
        ibut.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 // When on click, grab first user from queue and make him attack.
				for (int i = 0; i < fTeam.getPartySize(); i++) {
					if (fTeam.getCharacter(i).getReadyToAttack()) {
						fTeam.getCharacter(i).prepareForAttack();
					}
				}
			}
		});
        innerLayout.addView(ibut);
        
        /*
         * Magic button with image
         * 
         */
        matrix = new Matrix();
        
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.wand);
        width = bitmap.getWidth();
        height = bitmap.getHeight();
        scaleHeight = ((float) newHeight) / height;
        scaleWidth = ((float) newWidth) / width;
        ibut = new ImageButton(activity);
        matrix.postScale(scaleWidth, scaleHeight);
        ibut.setImageDrawable(new BitmapDrawable(Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true)));
        innerLayout.addView(ibut);
        
        /*
         * Shield button with image
         * 
         */
        matrix = new Matrix();
        
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.shield);
        width = bitmap.getWidth();
        height = bitmap.getHeight();
        scaleHeight = ((float) newHeight) / height;
        scaleWidth = ((float) newWidth) / width;
        ibut = new ImageButton(activity);
        matrix.postScale(scaleWidth, scaleHeight);
        ibut.setImageDrawable(new BitmapDrawable(Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true)));
        innerLayout.addView(ibut);

        /*
         * Backpack button with image
         * 
         */
        matrix = new Matrix();
        
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.backpack);
        width = bitmap.getWidth();
        height = bitmap.getHeight();
        scaleHeight = ((float) newHeight) / height;
        scaleWidth = ((float) newWidth) / width;
        ibut = new ImageButton(activity);
        matrix.postScale(scaleWidth, scaleHeight);
        ibut.setImageDrawable(new BitmapDrawable(Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true)));
        innerLayout.addView(ibut);

        /*
         * Shoe button with image
         * 
         */
        
        layout.setOnTouchListener(this);
        activity.setContentView(layout);
	}
	
	
	@Override
	public void onDraw(Canvas canvas) {
		 super.onDraw(canvas);
		 fTeam.onDraw(canvas);
		 eTeam.onDraw(canvas);
		 
		 fTeam.getCharacter(0).startTimer();
//		 fTeam.getCharacter(1).startTimer();
//		 fTeam.getCharacter(2).startTimer();
		 
		 this.invalidate();
     } 
	
	
	
	LinearLayout layout;
	
	BattleMenu battleMeButton;
	
	Team eTeam;
	Team fTeam;

	long moviestart;

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		Log.d("BattleView.onTouch", "I'm being touched at x,y" + event.getX() + " " + event.getY());
		for (int i = 0; i < fTeam.getPartySize(); i++) {
			if (fTeam.getCharacter(i).getReadyToAttack()) {
				for (int j = 0; j < eTeam.getPartySize(); j++)
					if (eTeam.getCharacter(j).gotTouched(event)) {
						fTeam.getCharacter(i).attack(eTeam.getCharacter(j));
					}
			}
		}
		return false;
	}
}      	
